package com.syqc.monitor.ui.cost

import android.view.WindowManager
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.gyf.immersionbar.ktx.immersionBar
import com.syqc.comlib.base.BaseActivity
import com.syqc.comlib.custom.MRadioToolbar
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityFeesBinding
import com.syqc.monitor.databinding.IncludeToolbarRadioBinding
import com.syqc.utils.Keys
import com.syqc.videos.CarMonitorViewModel

class FeeAddActivity : BaseActivity<ActivityFeesBinding>(ActivityFeesBinding::inflate) {
    private lateinit var mToolBarVB: IncludeToolbarRadioBinding
    private val mActivityVM by viewModels<CarMonitorViewModel>()
    override fun initView() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
        mToolBarVB = IncludeToolbarRadioBinding.bind(binding.root)
        immersionBar { titleBar(mToolBarVB.mCarRadioBar) }
        initToolbar()
        initIntent()
        initData()
    }

    private fun initIntent() {
        val carId = intent.getStringExtra(Keys.INTENT_CAR_ID)
        val carName = intent.getStringExtra(Keys.INTENT_PLATE) ?: ""
        if (carId == null) return
        mActivityVM.carId.postValue(carId)
        mActivityVM.carName.postValue(carName)
    }

    override fun setListener() {}

    private fun initData() {
        val fragments by lazy { mutableListOf<Fragment>() }
        fragments.add(CarFeeFragment())
        fragments.add(DriverFeeFragment())
        binding.vpFeeAdd.isUserInputEnabled = false
        binding.vpFeeAdd.adapter = FragmentStateAdapter(this, fragments)
    }


    private fun initToolbar() {
        mToolBarVB.mCarRadioBar.setTitle(
            R.string.desc_fee_type_car,
            R.string.desc_fee_type_driver
        )
        mToolBarVB.mCarRadioBar.setCallBack(object : MRadioToolbar.IToolbarCallback {
            override fun startCallback() {
                binding.vpFeeAdd.currentItem = 0
            }

            override fun endCallback() {
                binding.vpFeeAdd.currentItem = 1
            }
        })

    }
}

class FragmentStateAdapter(activity: AppCompatActivity, private val fragments: List<Fragment>) :
    FragmentStateAdapter(activity) {
    override fun getItemCount(): Int {
        return fragments.size
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }

}